import { WFileName,getFileName } from "../utils/tools.js";

let f_name = WFileName(import.meta.url);
let fileName = getFileName(f_name);

//添加模拟数据
let fruit = [
    {
        id:1,
        fruitName : '菠萝',
        price:'5.00'
    },
    {
        id:2,
        fruitName : '草莓',
        price:'5.10'
    },
    {
        id:3,
        fruitName : '哈密瓜',
        price:'9.00'
    },
    {
        id:4,
        fruitName : '香蕉',
        price:'2.50'
    },
    {
        id:5,
        fruitName : '油桃',
        price:'6.70'
    },
    {
        id:6,
        fruitName : '火龙果',
        price:'3.50'
    },
    {
        id:7,
        fruitName : '猕猴桃',
        price:'4.50'
    }
]


async function getAll(ctx){
    //获取从前端传来的参数
    let keyword = ctx.request.query.keyword || '';
    keyword = keyword.trim();
    //若关键字不为空，则使用关键字过滤表中数据
    if(keyword){
        let list = fruit.filter(f => {
            return f.fruitName.includes(keyword);
        });
        ctx.body={
            code:1000,
            data:list,
            msg:'显示查询水果'
        }
    }
    //否则直接返回全部数据
    else{
        ctx.body={
        code:1000,
        data:fruit,
        msg:'显示所有水果'
      }
    }
    
};
async function getSingle(ctx){
    //获取id，这里*1是为了将字符串转换为数值类型
    let id = ctx.request.params.id *1 || 0;
    //过滤获得指定id的数据
    let arr = fruit.filter(f => {
        return f.id === id;
    });
    let res = {
        code:1000,
        msg:`显示Id为${id}的水果`,
        data:arr[0]
    };
    ctx.body = res
};
async function postRole(ctx){
    //获取API后端的数据
    let obj = ctx.request.body;
    //获取最后一条数据的id，再加1就是新数据的id了
    let lastData = fruit[fruit.length-1];
    let lastId = lastData.id+1;
    //赋值给新数据id
    obj['id'] = lastId;
    //添加新数据
    fruit.push(obj);
    ctx.body = {
        code:1000,
        data:fruit,
        msg:'添加成功'
    };
};
async function putRole(ctx){
    let id = ctx.request.params.id*1 || 0;
    //获取api数据
    let apiData = ctx.request.body;
    let arr = fruit.filter(f => {
        return f.id === id;
    });
    //判断id是否存在，如果存在则进行修改
    if(arr.length > 0){
        let obj = arr[0];
        obj.fruitName = apiData.fruitName;
        obj.price = apiData.price;
        ctx.body = {
            code: 1000,
            msg: '修改成功'
        }
    }
    else{
    //如果不存在，则显示未找到
        ctx.body = {
            code:4000,
            msg:`Id为${id}的数据未找到，请确认是否输入错误`
        }
    }
};
async function delRole(ctx){
    let id = ctx.request.params.id *1 || 0;
    let arr = fruit.filter(f => {
        return f.id === id;
    });
    //查找要删除的数据的索引值
    let idx = fruit.findIndex(f => {
        return f.id === id;
    });
    if(arr.length > 0){
        //删除指定数据
        fruit.splice(idx,1);
        ctx.body = {
            code:1000,
            msg:`Id为${id}的数据删除成功`
        }
    }
    else{
        ctx.body = {
            code:4000,
            msg:`Id为${id}的数据未找到，请确认是否输入错误`
        }
    }
};

let obj = {}
obj[`get /${fileName}`]=getAll;
obj[`get /${fileName}/:id?`]=getSingle;
obj[`post /${fileName}`]=postRole;
obj[`put /${fileName}/:id?`]=putRole;
obj[`delete /${fileName}/:id?`]=delRole;

export default obj;